(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:loginDialog
     * @description
     * # loginDialog
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.pintuan.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('pintuanItem', ["$rootScope", "helper", function($rootScope, helper) {
            return {
                restrict: 'EA',
                template: "<a href='javascript:;' class=''  ng-click='showPro()'>+ {{'选择拼团商品'|translate}}",
                scope: {
                    chosedPintuans: "=",
                    callback: "&",
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', '$timeout', function($scope, authService, sessionService, dataService, $compile, helper, $templateCache, $timeout) {
                    $scope.pintuans = [];
                    $scope.productcat = {};
                    $scope.showPro = function() {
                        $scope.initData().then(showSelect)
                    }
                    $scope.initData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        $scope.page_size = 10;
                        // var user = sessionService.get("tokenUser");

                        var query = {};
                        query.content = {};

                        query.content.page_no = pageNo ? pageNo : "1";
                        query.content.page_size = $scope.page_size ? $scope.page_size : 10;
                        query.content.store_id = $rootScope.store_id;
                        return dataService.pintuanGet(query).then(function(r) {
                            if (r.Msg) {

                            } else {
                                $scope.pintuan_list_old = clone(r.pintuan_list);
                                async.map(r.pintuan_list, function(pintuan, cb) {
                                    pintuan.diff_time = $scope.diff_time(pintuan.start_time, pintuan.end_time)
                                    cb(null, null);
                                }, function(e, re) {
                                    $scope.pintuan_list = r.pintuan_list;
                                    $scope.bargain_rules_total_count = r.total_count;
                                    if ($scope.chosedPintuans) {
                                        if ($scope.chosedPintuans.length > 0) {
                                            $scope.pintuans = $scope.chosedPintuans
                                        }
                                    }
                                })


                            }
                        });
                    }

                    $scope.diff_time = function(start_time, end_time) {
                        var s_time = new Date().getTime();
                        var start = new Date(start_time).getTime();
                        var end = new Date(end_time).getTime();

                        //当前时间是否大于活动结束时间
                        if (s_time > end) {
                            return 0;
                        }

                        //判断当前时间是否大于开始时间
                        if (s_time > start && s_time < end) {
                            var time = new Date().getTime();
                        } else {
                            var time = new Date(start_time).getTime();
                        }

                        //var diff = end_time * 1000 - start_time * 1000 //时间差的毫秒数  
                        var diff = end - time;

                        //计算出相差天数  
                        var days = Math.floor(diff / (24 * 3600 * 1000));

                        //计算出小时数  
                        var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数  
                        var hours = Math.floor(leave1 / (3600 * 1000));
                        //计算相差分钟数  
                        var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数  
                        var minutes = Math.floor(leave2 / (60 * 1000));

                        //计算相差秒数  
                        var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数  
                        var seconds = Math.round(leave3 / 1000);

                        var returnStr = seconds + "秒";
                        if (minutes > 0) {
                            returnStr = minutes + "分" + returnStr;
                        }
                        if (hours > 0) {
                            returnStr = hours + "小时" + returnStr;
                        }
                        if (days > 0) {
                            returnStr = days + "天" + returnStr;
                        }
                        return returnStr;
                    }

                    this.closeWin = function() {
                        loadingToggle(true);
                        if ($scope.loginWin) {
                            $scope.loginWin.modal("hide");
                        }
                    }

                    function showSelect() {
                        $scope.error = null;

                        //弹出
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"拼团列表"|translate}}',
                            body: $templateCache.get("views/directives.pintuan.html"),
                            okBtn: '{{"确认"|translate}}',
                            closeBtn: true,
                            transition: true,
                            width: 'large',
                            height: 300,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if (_.size($scope.pintuans) > 0) {
                                        $scope.callback({
                                            items: $scope.pintuans
                                        });
                                    }
                                })
                            },
                            shown: function(e) {}
                        });
                    }
                    $scope.initProductcat = function(id) {
                        console.log(id);
                        dataService.fenxiaogradeOne(id).then(function(fenxiaograde) {
                            $scope.fenxiaograde = fenxiaograde;
                        });
                    }
                }],
                link: function(scope, elem, attrs, ctr) {
                    //  scope.$watch('ngModel', function(newValue, oldValue, scope) {
                    //     console.log(newValue,oldValue);
                    //     if(!_.isUndefined(newValue)&&newValue!=0){
                    //         scope.initProductcat(newValue);
                    //     }
                    //     // ctr.$setViewValue(newValue);
                    //     // scope.chosedItems = newValue;
                    // });
                }
            }
        }]);
})();